#!/usr/bin/perl
use strict;
use modules::encode;
package csaop;

sub csaop {
    my ($dbh,$remote,$input,$csnum) = @_;
    my ($sth, $auth);
    if ($$input[6] eq "") {
	print $remote "$csnum O $$input[0] :Syntax :AOP <#channel> <nick> <1/0>\n";
    }
    elsif ($$input[6] ne '1' && $$input[6] ne '0') {
	print $remote "$csnum O $$input[0] :Syntax :AOP <#channel> <nick> <1/0>\n";
    }

    else {
	$sth=$dbh->prepare("SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
	$sth->execute;
	$auth=$sth->fetchrow_array;
	if (!$auth) {
	    print $remote "$csnum O $$input[0] :You have to identify yourself with NickServ before using this command.\n";
	}
	else {
	    #GET ACCESS LVL
	    $sth=$dbh->prepare("SELECT level FROM users WHERE rnick = '$auth' AND level > 399 AND channel = '$$input[4]';");
	    $sth->execute;
	    my $lv = $sth->fetchrow_array;
	    if (!$lv) {
		print $remote "$csnum O $$input[0] :Access denied.\n";
	    }
	    else {
		$sth=$dbh->prepare("SELECT rnick FROM users WHERE rnick = '$$input[5]' AND channel = '$$input[4]' AND level > 99");
		$sth->execute;
		my $yes = $sth->fetchrow_array;
		if (!($yes)) {
		    print $remote "$csnum O $$input[0] :$$input[5] must have more than 99 access in $$input[4].\n";
		}
		else {
		    $sth=$dbh->prepare("UPDATE users SET aop = '$$input[6]' WHERE rnick = '$$input[5]' AND channel = '$$input[4]';");
		    $sth->execute;
		    print $remote "$csnum O $$input[0] :\cBAOP\cB for $$input[5] is now $$input[6].\n";
		}
	    }
	}
    }
}
1;
